package com.gaia.member.gaiatt.mygaia;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.gaia.member.gaiatt.R;
import com.gaia.member.gaiatt.bean.RecommandGoodsBean;
import com.gaia.member.gaiatt.utils.Uiutils;
import com.gaia.member.gaiatt.utils.ZhtUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhangHaiTao on 2016/5/6.
 * 我的盖亚  主界面
 */
public class MyGaiaHomeFragment extends Fragment {
    @Bind(R.id.gv_recommend_goods)
    CustomGridView gvRecommendGoods;
    @Bind(R.id.scroll_view)
    ScrollView scrollView;
    @Bind(R.id.ll_balance_entry)
    LinearLayout llBalanceEntry;
    @Bind(R.id.ll_favorable_ticket)
    LinearLayout llFavorableTicket;
    @Bind(R.id.ll_points_entry)
    LinearLayout llPointsEntry;
    @Bind(R.id.ll_my_order_entry)
    LinearLayout llMyOrderEntry;
    @Bind(R.id.ll_my_complaint_entry)
    LinearLayout llMyComplaintEntry;
    @Bind(R.id.ll_uncomment_order_entry)
    LinearLayout llUncommentOrderEntry;


    private Activity mContext;
    private RecommandGoodsAdapter recommandGoodsAdapter;
    private List<RecommandGoodsBean> recommandGoodsList = new ArrayList<RecommandGoodsBean>();
    private MyGaiaHomeClickListener myGaiaHomeClickListener = new MyGaiaHomeClickListener();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mygaia_home, null);
        ButterKnife.bind(this, view);
        mContext = getActivity();


        initData();

        initView();

        return view;
    }

    private void initData() {
        for (int i = 0; i < 7; i++) {
            RecommandGoodsBean recommandGoodsBean = new RecommandGoodsBean();
            recommandGoodsList.add(recommandGoodsBean);
        }
    }

    private void initView() {
        recommandGoodsAdapter = new RecommandGoodsAdapter(mContext, recommandGoodsList);
        gvRecommendGoods.setAdapter(recommandGoodsAdapter);
        gvRecommendGoods.setFocusable(false);//禁止scrollView自定滚动到GridView位置

        llBalanceEntry.setOnClickListener(myGaiaHomeClickListener);
        llFavorableTicket.setOnClickListener(myGaiaHomeClickListener);
        llPointsEntry.setOnClickListener(myGaiaHomeClickListener);
        llMyOrderEntry.setOnClickListener(myGaiaHomeClickListener);
        llMyComplaintEntry.setOnClickListener(myGaiaHomeClickListener);
        llUncommentOrderEntry.setOnClickListener(myGaiaHomeClickListener);


        gvRecommendGoods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Uiutils.showToast(mContext, "click " + position);
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
//        gvRecommendGoods.setFocusable(true);
    }

    class MyGaiaHomeClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_balance_entry:
                    ZhtUtils.gotoIntent(mContext, BalanceActivity.class);
                    break;
                case R.id.ll_favorable_ticket:
                    ZhtUtils.gotoIntent(mContext, CouponActivity.class);
                    break;
                case R.id.ll_points_entry:
                    ZhtUtils.gotoIntent(mContext, PointsActivity.class);
                    break;
                case R.id.ll_my_order_entry:
                    ZhtUtils.gotoIntent(mContext, MyOrderActivity.class);
                    break;
                case R.id.ll_my_complaint_entry:
                    ZhtUtils.gotoIntent(mContext, MyComplaintActivity.class);
                    break;
                case R.id.ll_uncomment_order_entry:
                    ZhtUtils.gotoIntent(mContext, UncommentOrderActivity.class);
                    break;
            }
        }
    }

    class RecommandGoodsAdapter extends BaseAdapter {
        Context context;
        List<RecommandGoodsBean> datas;

        public RecommandGoodsAdapter(Context context, List<RecommandGoodsBean> datas) {
            this.context = context;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.item_gird_recommand_goods, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            return convertView;
        }

        class ViewHolder {
            @Bind(R.id.iv_recommand_goods)
            ImageView ivRecommandGoods;
            @Bind(R.id.tv_goods_name)
            TextView tvGoodsName;
            @Bind(R.id.tv_goods_price)
            TextView tvGoodsPrice;
            @Bind(R.id.tv_goods_desc)
            TextView tvGoodsDesc;
            @Bind(R.id.btn_add_shopping_cart)
            Button btnAddShoppingCart;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
